﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Models;

namespace DAL
{
    public class Service
    {
        public static Models.ZhongChouContainer db = new ZhongChouContainer();
        private static List<Userinfo> userinfos = new List<Userinfo>();
        private static List<Projectinfo> projectinfos = new List<Projectinfo>();
        private static List<Returnsetinfo> returnsetinfos = new List<Returnsetinfo>();
        private static List<Launchinfo> launchinfos = new List<Launchinfo>();
        private static List<CollectionInfo> collectionInfos = new List<CollectionInfo>();
        private static List<Addressinfo> addressinfos = new List<Addressinfo>();
        
        
        /// <summary>
        /// 获取Userinfo数据源
        /// </summary>
        /// <param name="u">Userinfo类</param>
        /// <returns>返回对应数组</returns>
        public static List<Userinfo> GetDataSource(Userinfo u)
        {
            try
            {
                var UserDate = db.Userinfo.ToList();
                foreach (var item in UserDate)
                {
                    userinfos.Add(GetUser(item));
                }
                return userinfos;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
        }

        /// <summary>
        /// 获取Projectinfo数据源
        /// </summary>
        /// <param name="p">Projectinfo类</param>
        /// <returns>返回对应数组</returns>
        public static List<Projectinfo> GetDataSource(Projectinfo p)
        {
            try
            {
                var ProjectDate = db.Projectinfo.ToList();
                foreach (var item in ProjectDate)
                {
                    projectinfos.Add(GetProject(item));
                }
                return projectinfos;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
        }

        /// <summary>
        /// 获取Returnsetinfo数据源
        /// </summary>
        /// <param name="r">Returnsetinfo类</param>
        /// <returns>返回对应数组</returns>
        public static List<Returnsetinfo> GetDataSource(Returnsetinfo r)
        {
            try
            {
                var ReturnseDate = db.Returnsetinfo.ToList();
                foreach (var item in ReturnseDate)
                {
                    returnsetinfos.Add(GetReturnse(item));
                }
                return returnsetinfos;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
        }

        /// <summary>
        /// 获取Launchinfo数据源
        /// </summary>
        /// <param name="l">Launchinfo类</param>
        /// <returns>返回对应数组</returns>
        public static List<Launchinfo> GetDataSource(Launchinfo l)
        {
            try
            {
                var LaunchDate = db.Launchinfo.ToList();
                foreach (var item in LaunchDate)
                {
                    launchinfos.Add(GetLaunch(item));
                }
                return launchinfos;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
        }

        /// <summary>
        /// 获取CollectionInfo数据源
        /// </summary>
        /// <param name="c">CollectionInfo类</param>
        /// <returns>返回对应数组</returns>
        public static List<CollectionInfo> GetDataSource(CollectionInfo c)
        {
            try
            {
                var CollectionDate = db.CollectionInfo.ToList();
                foreach (var item in CollectionDate)
                {
                    collectionInfos.Add(GetCollection(item));
                }
                return collectionInfos;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
        }

        /// <summary>
        /// 获取Addressinfo数据源
        /// </summary>
        /// <param name="a">Addressinfo类</param>
        /// <returns>返回对应数组</returns>
        public static List<Addressinfo> GetDataSource(Addressinfo a)
        {
            try
            {
                var data = db.Addressinfo.ToList();
                foreach (var item in data)
                {
                    addressinfos.Add(GetAddress(item));
                }
                return addressinfos;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
        }


        /// <summary>
        /// 增删改
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <returns>返回操作是否成功</returns>
        public static bool EXEXE(string sql)
        {
            try
            {
                var sss = db.Database.ExecuteSqlCommand(sql);
                db.SaveChanges();
                return sss > 0;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
        }



        private static Addressinfo GetAddress(Addressinfo item)
        {
            return new Addressinfo()
            {
                address = item.address,
                addresscity = item.addresscity,
                addressid = item.addressid,
                postcode = item.postcode,
                userid = item.userid,
                username = item.username,
                userphone = item.userphone
            };
        }

        private static CollectionInfo GetCollection(CollectionInfo item)
        {
            return new CollectionInfo()
            {
                collectionid = item.collectionid,
                projectid = item.projectid,
                supportmoney = item.supportmoney,
                userid = item.userid
            };
        }

        private static Launchinfo GetLaunch(Launchinfo item)
        {
            return new Launchinfo()
            {
                projectid = item.projectid,
                accountname = item.accountname,
                address = item.address,
                bankname = item.bankname,
                banknumber = item.banknumber,
                launchid = item.launchid,
                launchname = item.launchname,
                phone = item.phone,
                subbranch = item.subbranch
            };
        }

        private static Returnsetinfo GetReturnse(Returnsetinfo item)
        {
            return new Returnsetinfo()
            {
                freight = item.freight,
                number = item.number,
                projectid = item.projectid,
                returncontent = item.returncontent,
                returnid = item.returnid,
                supportmoney = item.supportmoney,
                time = item.time
            };
        }

        private static Projectinfo GetProject(Projectinfo item)
        {
            return new Projectinfo()
            {
                aboutus = item.aboutus,
                collection = item.collection,
                createdate = item.createdate,
                imageurl = item.imageurl,
                incomemoney = item.incomemoney,
                need = item.need,
                other = item.other,
                projectadd = item.projectadd,
                projectdesc = item.projectdesc,
                projectid = item.projectid,
                projectlabel = item.projectlabel,
                projectname = item.projectname,
                promise = item.promise,
                risk = item.risk,
                showimgurl = item.showimgurl,
                state = item.state,
                targetmoney = item.targetmoney,
                time = item.time,
                type = item.type,
                userid = item.userid,
                videourl = item.videourl,
                wanttodo = item.wanttodo
            };
        }

        private static Userinfo GetUser(Userinfo item)
        {
            return new Userinfo()
            {
                address = item.address,
                imgurl = item.imgurl,
                registertime = item.registertime,
                sex = item.sex,
                userdesc = item.userdesc,
                userid = item.userid,
                usernikename = item.usernikename,
                userphone = item.userphone,
                userpwd = item.userpwd,
                userstate = item.userstate,
                website = item.website
            };
        }
    }
}